package com.lin.reggie.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.reggie.common.R;
import com.lin.reggie.entity.Employee;
import com.lin.reggie.entity.Page;
import com.lin.reggie.exception.BusinessException;
import com.lin.reggie.mapper.EmployeeMapper;
import com.lin.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class EmployeeServceImpl  implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;
    /**
     * 登录的业务方法
     *
     * @param employee
     * @return R
     */
    @Override
    public R<Employee> login(Employee employee) {
        //根据用户名查找用户
        Employee loginEmployee = employeeMapper.login(employee);
        //判断用户对象是否为null，如果为null说明用户不存在，提示给用户
        if (loginEmployee==null){
            throw new BusinessException("用户名或密码错误");
        }
        //将明文密码生成md5 跟数据库md5密码对比
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //判断两个md5是否一致
        if (!md5.equals(loginEmployee.getPassword())){
            throw new BusinessException("用户名或密码错误");
        }
        if (loginEmployee.getStatus()==0){
            throw new BusinessException("您的账户已被禁用，请联系管理员");
        }
        return R.success(loginEmployee);
    }
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //调用插件（PageHelper）设置查询第几页，每页多少条，自动生成sql语句
        PageHelper.startPage(page,pageSize);
        //执行查询所有数据的sql语句方法
        List<Employee> employeeList = employeeMapper.findByName(name);
        //使用PageInfo封装上面的结果
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        Page<Employee> PageObj = new Page<>();
        PageObj.setRecords(pageInfo.getList());
        PageObj.setTotal(pageInfo.getTotal());
        return PageObj;
    }

    @Override
    public boolean save(Employee employee) {
        //获取用户名，校验是否存在
        Employee loginUser = employeeMapper.login(employee);
        //
        if (loginUser!=null){
            throw new BusinessException("该账户已被注册！");
        }
        //完善信息
        //默认密码  用工具类加密成MD5
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置状态码
        employee.setStatus(1);
        //创建时间
        employee.setCreateTime(LocalDateTime.now());
        //修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //调用mapper添加到数据库
        employeeMapper.save(employee);
        return true;
    }

    //启用禁用按钮
    @Override
    public boolean status(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        Employee dbUserName = employeeMapper.fingByUserName(employee.getUsername());
        if (dbUserName != null && dbUserName.getId()!=employee.getId()) {
            throw new BusinessException("该用户名已经存在，请重新输入");
        }
        return employeeMapper.update(employee)>0;
    }

    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return R.success(employee);
    }
}
